#include "ultrasonic.h"
#include "rgb.h"
#include "steer.h"
#include "motor.h"
#include <pthread.h>
//#include <e_file.h>
#include <string.h>

//超声波相关变量的定义
struct gpio_desc gpio_val[6];
struct gpio_desc gpios;
char gpio_num[6];//管脚编号
char pptr[128];
char buff4_4[BUFFSIZE];
char buff4_5[BUFFSIZE];
int trash_distance = 0;//接收超声波测距的返回值

//PWM_RGB相关变量的定义
unsigned long duty1 = 0;//0
unsigned long duty2 = 1000000;//1000000
unsigned long duty = 0;//0
int increase = 1;
int step;//1000;
char dutys[256];
char pwm0_path[128];
char pwm2_path[128];

//舵机相关变量的定义
int uart_fd;
char buf_steer[8];

//步进电机相关变量定义
char buff4_1[BUFFSIZE];
char buff4_3[BUFFSIZE];
char buff4_11[BUFFSIZE];
char buff5_12[BUFFSIZE];
char buff5_13[BUFFSIZE];
char buff5_14[BUFFSIZE];

//线程的相关变量定义
pthread_t tid;
int err;

//舵机和步进电机的状态标记位
int steer_statu = 0;
int motor_statu = 0;

//线程要执行的工作
static void *new_thr_job (void *s)
{
	struct pollfd fds4_1[1];
	struct pollfd fds4_3[1];
	int gpio_value_fd4_1, gpio_value_fd4_3;
	int ret = 0;

	//设置key1、key2监听
	//当key1中断发生时, 唤醒poll进程读取value的值
	gpio_value_fd4_1 = open (buff4_1, O_RDONLY);
	if (gpio_value_fd4_1 == -1) {
		perror ("open value_key1 file error");
		exit (-1);
	}
	fds4_1[0].fd = gpio_value_fd4_1;
	fds4_1[0].events = POLLPRI;
	ret = read (gpio_value_fd4_1, buff4_1, 10);
	if (-1 == ret) {
		perror ("read value_key1 file error");
		exit (-1);
	}

	//当key2中断发生时, 唤醒poll进程读取value的值
	gpio_value_fd4_3 = open (buff4_3, O_RDONLY);
	if (gpio_value_fd4_3 == -1) {
		perror ("open value_key2 file error");
		exit (-1);
	}
	fds4_3[0].fd = gpio_value_fd4_3;
	fds4_3[0].events = POLLPRI;
	ret = read (gpio_value_fd4_3, buff4_3, 10);
	if (-1 == ret) {
		perror ("read value_key2 file error");
		exit (-1);
	}
	while (1) {
		//监听key1和key2
		if ((ret = poll(fds4_1, 1, 0))) {
			if (fds4_1[0].revents & POLLPRI) {
				ret = lseek (gpio_value_fd4_1, 0, SEEK_SET);
				if (-1 == ret) {
					perror ("lseek error");
				}
				memset (buff4_1, 0, sizeof(BUFFSIZE)/sizeof(char));
				ret = read (gpio_value_fd4_1, buff4_1, 10);
				if (-1 == ret) {
					perror ("read error!");
				}
				printf ("press key1\n");
				if (motor_statu < 2) {
					motor_statu = motor_statu + 1;
				} else {
					motor_statu = 0;
					motor_init();
				}
				ret = 0;
			}
		}
		if ((ret = poll (fds4_3, 1, 0))) {
			if (fds4_3[0].revents & POLLPRI) {
				ret = lseek (gpio_value_fd4_3, 0, SEEK_SET);
				if (-1 == ret) {
					perror ("lseek error");
				}
				memset (buff4_3, 0, sizeof(BUFFSIZE)/sizeof(char));
				ret = read (gpio_value_fd4_3, buff4_3, 10);
				if (-1 == ret) {
					perror ("read error!");
				}
				printf ("press key2\n");
				if (steer_statu < 1) {
					steer_statu = steer_statu + 1;
				} else {
					steer_statu = 0;
					steer_0degree();
				}
				ret = 0;
			}
		}
	}
	//close (gpio_value_fd4_1);
	//close (gpio_value_fd4_3);
	//return 0;
}


int main ()
{
	step = (int)((duty2 - duty1) / 10);//1000
	//gpio初始化
	gpio_init ();
	//使能pwm控制器
	PWM_gpio_mode (PWM_ENABLE_REG, WRITE_TYPE_H, PWM_ENABLE_VAL);
	//配置gpio管脚的模式
	PWM_gpio_mode (GPIO_PAD_MOOE_PWM0_CHANNEL0_REG, WRITE_TYPE_H, GPIO_PAD_MOOE_PWM0_CHANNEL0_VAL);
	PWM_gpio_mode (GPIO_PAD_MOOE_PWM2_CHANNEL0_REG, WRITE_TYPE_H, GPIO_PAD_MOOE_PWM2_CHANNEL0_VAL);
	
	//超声波
	config_gpio (4, 4, buff4_4);
	config_gpio (4, 5, buff4_5);
	//步进电机
	config_gpio (4, 11, buff4_11);
	config_gpio (5, 12, buff5_12);
	config_gpio (5, 13, buff5_13);
	config_gpio (5, 14, buff5_14);
	//按键
	config_gpio (4, 1, buff4_1);
	config_gpio (4, 3, buff4_3);

	//将pwm0和pwm2的节点文件导出到用户空间
	//注意这个函数应当在pwm_config()和set_pwm_value()前调用!!!!!
	set_pwm_export ();
	//配置pwm period,duty_cycle,enable
	set_pwm_value ();
	rgb_init ();
	
	//舵机初始化
	uart_init();
	//创建一个线程
	if (err = pthread_create (&tid, NULL, new_thr_job, NULL)) {
		fprintf (stderr, "pthread_create():%s\n", strerror(err));
		exit (1);
	}

	while (1) {
		send_ultrasonic ();
		
		printf("test1\n");
		trash_distance = get_diatance_value ();
		
		rgb_init ();
		
		
		if (trash_distance < 10) {
			red_blue_mode ();
			//motor_l ();
			//steer_90degree ();
		}  
		if (trash_distance >= 10) {
			breath_mode ();
			//steer_0degree ();
		}
		while (motor_statu == 1) {
			motor_l();
		}
		while (motor_statu == 2) {
			motor_r();
		}
		/*while (motor_statu == 3) {
			motor_init();
		}*/
		while (steer_statu == 1) {
			steer_90degree();
		}
		/*while (steer_statu == 2) {
			steer_0degree();
		}*/

		//舵机自动模式，按下key3转至90度，延时一段时间后恢复到0度
		steer_auto ();
		//usleep (100000);
	}
	return 0;
}
